#!/bin/bash
set -e

function main() {
    uninstall_fluentd
    uninstall_kibana
    uninstall_elasticsearch
    uninstall_nfs
    uninstall_init
}

function uninstall_fluentd() {
  cd ch4_fluentd
  sudo kubectl delete -f fluentd.yaml --wait=true
  echo uninstall_fluentd completed
  cd ..
}

function uninstall_kibana() {
  cd ch3_kibana
  sudo kubectl delete -f kibana.yaml --wait=true
  echo uninstall_kibana completed
  cd ..
}

# 卸载elasticsearch
function uninstall_elasticsearch() {
  cd ch2_elasticsearch
  sudo kubectl delete -f elasticsearch_svc.yaml --wait=true
  # 删除测试svc
  #sudo kubectl delete -f elasticsearch_svc_nodeport.yaml --wait=true
  # 删除单节点
  sudo kubectl delete -f elasticsearch-statefulset-single.yaml --wait=true
  # 删除es 集群节点
  #sudo kubectl delete -f elasticsearch-statefulset-ha.yaml --wait=true
  echo uninstall_elasticsearch completed
  cd ..
}


# 卸载nfs
function uninstall_nfs() {
  cd ch1_nfs
  sudo kubectl delete -f nfs_serviceaccount.yaml --wait=true
  sudo kubectl delete -f nfs-rbac.yaml --wait=true
  sudo kubectl delete -f nfs-deployment.yaml --wait=true
  sudo kubectl delete -f nfs-class.yaml --wait=true
  echo uninstall_nfs completed
  cd ..
}

function uninstall_init() {
  cd ch0_init
  sudo kubectl delete -f busybox-pod.yaml --wait=true
  # 检测
  sudo kubectl -n kube-logging get pod,svc,ep
  # 名字空间删除会花费比较长的时间，理由是名称空间删除需要关闭集群中的所有资源，这需要许多小步骤，因此可能需要一段时间才能完成
  #sudo kubectl delete -f kube-logging-namespace.yaml --wait=true
  echo uninstall completed
}

main